package com.bear.sgg.springboot_11_cache.config;

import com.bear.sgg.springboot_11_cache.entity.MyJacksonSerializableObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.cache.CacheManagerCustomizers;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.autoconfigure.cache.RedisCacheManagerBuilderCustomizer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.net.UnknownHostException;
import java.time.Duration;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * @ClassNmae RedisConfig
 * @Description TODO
 * @Author bear
 * @Date 2020/3/17 0017 下午 5:19
 */
@Configuration
public class RedisConfig {

    /**
     * 自定义redis操作模板（非注解方式有效，参考测试用例）
     * @param redisConnectionFactory
     * @return
     * @throws UnknownHostException
     */
    @Bean
    public RedisTemplate<Object, MyJacksonSerializableObject> myJsonRedisTemplate(RedisConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate<Object, MyJacksonSerializableObject> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        //配置使用json序列化器
        template.setDefaultSerializer(new Jackson2JsonRedisSerializer<>(MyJacksonSerializableObject.class));
        return template;
    }

    /**
     * 自定义redis的cache manager
     * 当使用@Cacheable等注解进行缓存时，上述自定义的RedisTemplate并不会生效的，需要配置RedisCacheManager的默认配置才行
     * @param factory
     * @return
     */
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        Jackson2JsonRedisSerializer<MyJacksonSerializableObject> jackson2JsonRedisSerializer =
                new Jackson2JsonRedisSerializer<MyJacksonSerializableObject>(MyJacksonSerializableObject.class);
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofHours(1))  // 设置缓存有效期一小时
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer));  //选择jackson序列化方式
        return RedisCacheManager
                .builder(factory)
                .cacheDefaults(redisCacheConfiguration).build();
    }

}
